package com.zs.servlet;

import com.zs.factory.ActionFactory;
import com.zs.service.Action;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/6/11 上午10:09
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("UTF-8");
       response.setContentType("text/html;charset=UTF-8");
       response.setCharacterEncoding("UTF-8");
       String opr = request.getParameter("opr");
      Action action = ActionFactory.createAction(opr);
        try {
            Method method = action.getClass().getMethod("execute",HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(action,request,response);
//            action.execute(request, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doGet(req,resp);
    }
}
